package com.bnq.logc.upload

import cn.ucloud.ufile.api.ApiError
import cn.ucloud.ufile.bean.PutObjectResultBean
import cn.ucloud.ufile.bean.UfileErrorBean
import cn.ucloud.ufile.http.BaseHttpCallback
import com.tencent.cos.xml.exception.CosXmlClientException
import com.tencent.cos.xml.exception.CosXmlServiceException
import com.tencent.cos.xml.model.CosXmlResult
import okhttp3.Request
import java.io.File

interface AbstractFileUploadManager<SuccessResult,Exception1Result,Exception2Result> {
    fun upload(file: File,callback:FileUploadResultCallback<SuccessResult,Exception1Result,Exception2Result>):String
    fun uploadCommon(file: File,cloudPathName:String,callback:FileUploadResultCallback<SuccessResult,Exception1Result,Exception2Result>):String
}

class UfileFileUploadManager : AbstractFileUploadManager<PutObjectResultBean?, ApiError?, UfileErrorBean?>{
    override fun upload(
        file: File,
        callback: FileUploadResultCallback<PutObjectResultBean?, ApiError?, UfileErrorBean?>
    ):String {
        return UfileClientManager.upload(file,object : BaseHttpCallback<PutObjectResultBean, UfileErrorBean>(){
            /**
             * Response回调
             *
             * @param response response，泛型指定
             */
            override fun onResponse(response: PutObjectResultBean?) {
                callback.onSuccess(response)
            }

            override fun onProgress(bytesWritten: Long, contentLength: Long) {
                super.onProgress(bytesWritten, contentLength)
            }

            /**
             * Error回调
             *
             * @param request  请求体，部分情况的Error会返回null
             * @param error    错误类,[ApiError]，onError的主要判断依据
             * @param response response，泛型指定，部分情况无法正常解析出指定的Response，会返回null
             */
            override fun onError(
                request: Request?,
                error: ApiError?,
                response: UfileErrorBean?
            ) {
                callback.onFail(error,response)
            }
        })
    }

    override fun uploadCommon(
        file: File,
        cloudPathName: String,
        callback: FileUploadResultCallback<PutObjectResultBean?, ApiError?, UfileErrorBean?>
    ):String {
        return UfileClientManager.uploadCommon(file,cloudPathName,object : BaseHttpCallback<PutObjectResultBean, UfileErrorBean>(){
            /**
             * Response回调
             *
             * @param response response，泛型指定
             */
            override fun onResponse(response: PutObjectResultBean?) {
                callback.onSuccess(response)
            }

            override fun onProgress(bytesWritten: Long, contentLength: Long) {
                super.onProgress(bytesWritten, contentLength)
            }

            /**
             * Error回调
             *
             * @param request  请求体，部分情况的Error会返回null
             * @param error    错误类,[ApiError]，onError的主要判断依据
             * @param response response，泛型指定，部分情况无法正常解析出指定的Response，会返回null
             */
            override fun onError(
                request: Request?,
                error: ApiError?,
                response: UfileErrorBean?
            ) {
                callback.onFail(error,response)
            }
        })
    }
}

class CosFileUploadManager : AbstractFileUploadManager<CosXmlResult?, CosXmlClientException?, CosXmlServiceException?>{
    override fun upload(
        file: File,
        callback: FileUploadResultCallback<CosXmlResult?, CosXmlClientException?, CosXmlServiceException?>
    ):String {
        return CosFileClientManager.upload(file, callback)
    }

    override fun uploadCommon(
        file: File,
        cloudPathName: String,
        callback: FileUploadResultCallback<CosXmlResult?, CosXmlClientException?, CosXmlServiceException?>
    ):String {
        return CosFileClientManager.uploadCommon(file, cloudPathName, callback)
    }

}